package org.september.pisces.operation.controller;

import org.september.pisces.operation.enums.OperationSettingEnum;
import org.september.pisces.settings.service.PiscesSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.*;

@Controller
public class DbBackupController {

    @Autowired
    PiscesSettingService settingService;

    @RequestMapping(List_Page)
    public ModelAndView backupList() {
        ModelAndView mv = new ModelAndView();
        String saveFilePath = settingService.getValueOrDefault(OperationSettingEnum.数据库备份路径);
        File saveDir = new File(saveFilePath);
        List<String> fileNames = new ArrayList<>();
        if (saveDir.exists()) {
            List<File> listFile = Arrays.stream(Objects.requireNonNull(saveDir.listFiles())).toList();
            fileNames = listFile.stream().map(File::getName).sorted(Comparator.reverseOrder()).toList();
        }
        mv.addObject("fileNames", fileNames);
        return mv;
    }

    private static final String Mapping_Prefix = "/dbBackup";
    public static final String List_Page = Mapping_Prefix + "/backupList";
}
